package com.dhcc.DRGsHosp.application.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.querychannel.JdbcTemplateObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.ScheduleJobApplication;
import com.dhcc.DRGsHosp.core.domain.ScheduleJob;

/**
 * @ClassName: ScheduleJobApplicationImpl  
 * @Description ：    定时任务--数据访问层
 * @Date: 2018年10月14日 下午3:32:08  
 * @Author QiaoYiLong 
 * @Version 3.1.1 DHC-DRG
 */
@Named
@Transactional
public class ScheduleJobApplicationImpl implements ScheduleJobApplication {
	
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	
	/**
	 * 添加任务
	 */
	@Override
	public void creatScheduleJob(ScheduleJob scheduleJob) {
		scheduleJob.save();
	}
	/**
	 * 修改任务
	 */	
	@Override
	public void updateScheduleJob(ScheduleJob scheduleJob) {
		scheduleJob.save();
	}
	/**
	 * 删除任务
	 */	
	@Override
	public void removeScheduleJob(ScheduleJob scheduleJob) {
		if( null != scheduleJob ){
			scheduleJob.remove();
		}
	}
	/**
	 * 删除多个任务
	 */
	@Override
	public void removeScheduleJobs(Set<ScheduleJob> scheduleJobs) {
		for(ScheduleJob scheduleJob : scheduleJobs){
			if( null != scheduleJob ){
				scheduleJob.remove();
			}
		}
	}

	/**
	 * 根据任务id取得对应的任务信息
	 */	
	@Override
	public ScheduleJob getScheduleJob(String id) {
		ScheduleJob scheduleJob = null ;
		if(null!= id && !"".equals(id) && id.trim().length()>0){
			String sql = "	select t.ID \"jobId\",t.JOB_NAME \"jobName\",t.JOB_GROUP_NAME \"jobGroup\",t.JOB_STATUS \"jobStatus\",  \n "
					+ "	t.CRON \"cronExpression\",t.DESCRIPTION \"description\",t.BEAN_NAME \"beanClass\",  \n "
					+ "	t.METHOD_NAME \"methodName\",t.JOB_DATA \"jobData\",t.CREATE_DATE \"createTime\",t.MODIFY_DATE \"updateTime\",t.job_execute_time \"jobExecuteTime\"   \n "
					+ " from DRGS.T_TIMETASK t where t.id = '"+id+"'";
			List<ScheduleJob> sJobList = jdbcTemplateObj.query(sql,new Object[]{}, new BeanPropertyRowMapper<ScheduleJob>(ScheduleJob.class));
			if(null != sJobList && sJobList.size()>0){
				scheduleJob = sJobList.get(0);
			}
		}
		return scheduleJob;
	}
	/**
     *  任务名称是否已经存在
     */
	@Override
	public boolean isScheduleJobExist(String jobName) {
		boolean exist = false;
		if(null!= jobName && !"".equals(jobName) && jobName.trim().length()>0){
			String sql = "	select count(1) from DRGS.T_TIMETASK t where t.JOB_NAME = '"+jobName+"'";
			int count = jdbcTemplateObj.queryForObject(sql, Integer.class);
			if(count>0){
				exist = true;
			}
		}
		return exist;
	}
	/**
	 * 获取符合查询条件的数据
	 */
	@Override
	public List<ScheduleJob> findAllScheduleJobByCondition(ScheduleJob scheduleJob) {
		List<ScheduleJob> sJobList = new ArrayList<ScheduleJob>();
		StringBuffer sql = new StringBuffer();
		sql.append("	select t.ID \"jobId\",t.JOB_NAME \"jobName\",t.JOB_GROUP_NAME \"jobGroup\",t.JOB_STATUS \"jobStatus\",  \n ") ; 
		sql.append("	t.CRON \"cronExpression\",t.DESCRIPTION \"description\",t.BEAN_NAME \"beanClass\",  \n "); 
		sql.append("	t.METHOD_NAME \"methodName\",t.JOB_DATA \"jobData\",t.CREATE_DATE \"createTime\",t.MODIFY_DATE \"updateTime\"   \n ");
		sql.append(" 	from DRGS.T_TIMETASK t where 1=1 ");
		
		if(null!= scheduleJob && null!= scheduleJob.getJobId() && !"".equals(scheduleJob.getJobId()) && scheduleJob.getJobId().trim().length()>0){
			sql.append(" and t.ID = '"+scheduleJob.getJobId()+"' ");
		}
		if(null!= scheduleJob && null!= scheduleJob.getJobName() && !"".equals(scheduleJob.getJobName()) && scheduleJob.getJobName().trim().length()>0){
			sql.append(" and t.JOB_NAME = '"+scheduleJob.getJobName()+"' ");
		}
		if(null!= scheduleJob && null!= scheduleJob.getJobGroup() && !"".equals(scheduleJob.getJobGroup()) && scheduleJob.getJobGroup().trim().length()>0){
			sql.append(" and t.JOB_GROUP_NAME = '"+scheduleJob.getJobGroup()+"' ");
		}
		if(null!= scheduleJob && null!= scheduleJob.getJobStatus() && !"".equals(scheduleJob.getJobStatus()) && scheduleJob.getJobStatus().trim().length()>0){
			sql.append(" and t.JOB_STATUS = '"+scheduleJob.getJobStatus()+"' ");
		}
		
		sJobList = jdbcTemplateObj.query(sql.toString(),new Object[]{}, new BeanPropertyRowMapper<ScheduleJob>(ScheduleJob.class));
		return sJobList;
	}

	/**
	 * 获取符合查询条件的数据
	 */
	@Override
	public List<Map<String,Object>> findAllByCondition(Map<String,Object> condition) {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		StringBuffer sql = new StringBuffer();
		sql.append("	select t.ID \"jobId\",t.JOB_NAME \"jobName\",t.JOB_GROUP_NAME \"jobGroup\",t.JOB_STATUS \"jobStatus\",  \n ") ; 
		sql.append("	t.CRON \"cronExpression\",t.DESCRIPTION \"description\",t.BEAN_NAME \"beanClass\",  \n "); 
		sql.append("	t.METHOD_NAME \"methodName\",t.JOB_DATA \"jobData\",t.CREATE_DATE \"createTime\",t.MODIFY_DATE \"updateTime\"   \n ");
		sql.append(" 	from DRGS.T_TIMETASK t where 1=1 ");
		
		if(null!= condition && condition.containsKey("jobId") && !"".equals(""+condition.get("jobId")) ){
			sql.append(" and t.ID = '"+condition.get("jobId")+"' ");
		}
		if(null!= condition && condition.containsKey("jobName") && !"".equals(""+condition.get("jobName")) ){
			sql.append(" and t.JOB_NAME = '"+condition.get("jobName")+"' ");
		}
		if(null!= condition && condition.containsKey("jobGroup") && !"".equals(""+condition.get("jobGroup")) ){
			sql.append(" and t.JOB_GROUP_NAME = '"+condition.get("jobGroup")+"' ");
		}
		if(null!= condition && condition.containsKey("jobStatus") && !"".equals(""+condition.get("jobStatus")) ){
			sql.append(" and t.JOB_STATUS = '"+condition.get("jobStatus")+"' ");
		}
		
		list = jdbcTemplateObj.queryForList(sql.toString());
		return list;
	}

}
